package com.wbihua.metrics.metric;

import com.wbihua.common.metrics.type.EventGaugeType;
import com.wbihua.common.metrics.metric.EventMeterGauge;
import com.wbihua.common.metrics.manager.MetricManager;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Gauge;

public class PrometheusMetricGauge implements EventMeterGauge {
    private final EventGaugeType counterType;

    private Gauge gauge;

    public PrometheusMetricGauge(EventGaugeType counterType) {
        this.counterType = counterType;
    }
    @Override
    public void registry(MetricManager metricManager) {
        CollectorRegistry prometheusRegistry =((PrometheusMeterRegistry)metricManager.getMetricRegistry()).getPrometheusRegistry();
        this.gauge = Gauge.build()
                .name(this.counterType.getName())
                .labelNames(this.counterType.getLabelNames())
                .help(this.counterType.getHelp()).register(prometheusRegistry);
    }

    @Override
    public <T> T getMeter() {
        return (T)this.gauge;
    }

    @Override
    public <T> T getMeter(String... tagValues) {
        return (T)this.gauge.labels(tagValues);
    }

    @Override
    public EventGaugeType getCounterType() {
        return this.counterType;
    }

    @Override
    public void increment(double count) {
        this.gauge.inc(count);
    }

    @Override
    public void increment(double count, String... labelValues) {
        this.gauge.labels(labelValues).inc(count);
    }

    @Override
    public void decrement(double count) {
        this.gauge.dec(count);
    }

    @Override
    public void decrement(double count, String... labelValues) {
        this.gauge.labels(labelValues).dec(count);
    }
}
